﻿using Compass.Maui.Services.FinishGoods;

namespace Compass.Maui.ViewModels.FinishGoods;
[QueryProperty(nameof(PalletDto),nameof(PalletDto))]
public partial class WeighingPalletPageViewModel : BaseViewModel
{
    private readonly IPalletService _palletService;
    public WeighingPalletPageViewModel(IPalletService palletService)
    {
        _palletService = palletService;
    }


    [ObservableProperty]
    private PalletDto palletDto;

    [RelayCommand]
    private async Task GoBackAsync()
    {
        await Shell.Current.GoToAsync("..");
        //await Shell.Current.Navigation.PopModalAsync();
    }
    [RelayCommand]
    private async Task SaveAsync()
    {
        if (PalletDto.Id != null && PalletDto.Id != Guid.Empty)
        {
            //更新
            await _palletService.UpdateWithEventAsync(PalletDto.Id.Value, PalletDto);
        }
        else
        {
            //添加
            await _palletService.AddAsync(PalletDto);
        }
        

        await GoBackAsync();
    }
    [RelayCommand]
    private async Task DeleteAsync()
    {
        //提示是否删除
        var result=  await Shell.Current.DisplayAlert("删除提示", $"确定要删除{PalletDto.PalletNumber}号托盘:{PalletDto.MtlNumber}吗？", "确定","取消");
        if (result)
        {
            if (PalletDto.Id != null && PalletDto.Id != Guid.Empty)
            {
                await _palletService.DeleteAsync(PalletDto.Id.Value);
            }
            await GoBackAsync();
        }
    }
}